import { defineStore } from 'pinia';
import { loginApi as adminLogin, userLoginApi } from '/@/api/user';
import { setToken, clearToken } from '/@/utils/auth';
import { UserState } from './types';
import {
  USER_ID,
  USER_NAME,
  USER_TOKEN,
  USER_NICK_NAME,
  USER_AVATAR,
  ADMIN_USER_ID,
  ADMIN_USER_NAME,
  ADMIN_USER_TOKEN,
  ADMIN_USER_ROLE,
  AUTHORIZATION,
} from '/@/store/constants';

export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    userId: undefined,
    userName: undefined,
    nickName: undefined,
    userToken: undefined,
    avatar: undefined,

    admin_user_id: undefined,
    admin_user_name: undefined,
    admin_user_token: undefined,
    admin_user_role:undefined,
  }),
  getters: {},
  actions: {
    setUSerInfo(result) {
      this.$patch((state) => {
        state.userId = result.id;
        state.userName = result.username;
        state.userToken = result.token;
        state.nickName = result.nickname;
        state.avatar = result.avatar;
        console.log('state==>', state);
      });
      console.log(result);
      localStorage.setItem(USER_TOKEN, result.token);
      localStorage.setItem(USER_NAME, result.username);
      localStorage.setItem(USER_ID, result.id);
      localStorage.setItem(USER_NICK_NAME, result.nickname);
      localStorage.setItem(USER_AVATAR, result.avatar);
    },
    // 用户登录
    async login(loginForm) {
      const result = await userLoginApi(loginForm);
      console.log('result==>', result);

      if (result.code === 200) {
        this.$patch((state) => {
          state.userId = result.data.id;
          state.userName = result.data.username;
          state.userToken = result.data.token;
          state.nickName = result.data.nickname;
          state.avatar = result.data.avatar;
          console.log('state==>', state);
        });
        localStorage.setItem(USER_TOKEN, result.data.token);
        localStorage.setItem(USER_NAME, result.data.username);
        localStorage.setItem(USER_ID, result.data.id);
        localStorage.setItem(USER_NICK_NAME, result.data.nickName);
        localStorage.setItem(USER_AVATAR, result.data.avatar);
      }

      return result;
    },
    // 用户登出
    async logout() {
      // await userLogout();
      this.$patch((state) => {
        localStorage.removeItem(USER_ID);
        localStorage.removeItem(USER_NAME);
        localStorage.removeItem(USER_TOKEN);
        localStorage.removeItem(USER_NICK_NAME);
        localStorage.removeItem(USER_AVATAR);

        state.userId = undefined;
        state.userName = undefined;
        state.userToken = undefined;
        state.nickName = undefined;
        state.avatar = undefined;
      });
    },

    // 管理员登录
    async adminLogin(loginForm) {
      const result = await adminLogin(loginForm);
      console.log('result==>', result);

      if (result.code === 200) {
        this.$patch((state) => {
          state.admin_user_id = result.content.id;
          state.admin_user_name = result.content.username;
          state.admin_user_token = result.content.token;
          state.admin_user_role = "3";//todo fix 固定是管理员，可以查看所有菜单
          console.log('state==>', state);
        });
        // todo 临时固定些死的 Bearer Token，注意，需要有效的token
        const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTc3NzQxMjUsInVzZXJfbmFtZSI6InRvbSIsImp0aSI6IjNhMGM5YjdkLWNiZmUtNGVmNy1iODlmLWQ5NjNjYTdkOGQ5ZSIsImNsaWVudF9pZCI6Im15LXRydXN0ZWQtY2xpZW50Iiwic2NvcGUiOlsicmVhZCIsIndyaXRlIiwidHJ1c3QiXX0.my76deT52K_u4tAzINdSaBf13mlgyPKONvT74HVvYf0';
        localStorage.setItem(AUTHORIZATION, token);
        localStorage.setItem(ADMIN_USER_TOKEN, result.content.token);
        localStorage.setItem(ADMIN_USER_NAME, result.content.username);
        localStorage.setItem(ADMIN_USER_ID, result.content.id);
        localStorage.setItem(ADMIN_USER_ROLE, "3")
        console.log('localStorage==>', localStorage);
      }

      return result;
    },
    // 管理员登出
    async adminLogout() {
      // await userLogout();
      this.$patch((state) => {
        localStorage.removeItem(ADMIN_USER_ID);
        localStorage.removeItem(ADMIN_USER_NAME);
        localStorage.removeItem(ADMIN_USER_TOKEN);
        localStorage.removeItem(ADMIN_USER_ROLE)

        state.admin_user_id = undefined;
        state.admin_user_name = undefined;
        state.admin_user_token = undefined;
      });
    },
  },
});
